<template>
  <!-- isRood ? '编辑用户': -->
  <el-dialog
    :title="modifyData.id ? '编辑用户':'添加用户'"
    :visible="dialogVisible"
    width="30%"
    :before-close="ondelS"
  >
    <el-form ref="addUser" :model="addUser" :rules="rules" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="addUser.username" :disabled="modifyData.id ? true:false" />
      </el-form-item>
      <el-form-item v-if="!modifyData.id" label="密码" prop="password">
        <el-input v-model="addUser.password" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="addUser.email" />
      </el-form-item>
      <el-form-item label="电话" prop="mobile">
        <el-input v-model="addUser.mobile" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="ondelS">取 消</el-button>
      <el-button type="primary" @click.native="clickAdd">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { putEditUser } from '@/api/Support.js'
export default {
  name: 'SpringFrame',
  model: {
    prop: 'dialogVisible'
  },
  props: {
    // 添加用户判断
    dialogVisible: {
      type: Boolean,
      required: true
    },
    // 编辑用户数据
    modifyData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      // 添加用户
      addUser: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 表单验证
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,10}/,
            message: '最少6个最多十个字符，至少1个大写字母，1个小写字母，1个数字和1个特殊字符',
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { pattern: /^[\w\-]+@[a-zA-Z\d\-]+(\.[a-zA-Z]{2,8}){1,16}$/,
            message: '格式有误，请检查后重新输入', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.$nextTick(function() {
      this.addUser = this.modifyData
      console.log(this.modifyData)
    })
  },
  methods: {
    // 关闭弹框
    ondelS() {
      this.$emit('input', false)
      this.$emit('clickId')
      this.$refs.addUser.resetFields()
      this.addUser = {
        username: '',
        password: '',
        email: '',
        mobile: ''
      }
    },
    // 点击确认
    async clickAdd() {
      await this.$refs.addUser.validate()
      if (this.modifyData.id) {
        const res = await putEditUser(this.modifyData.id, this.addUser)
        if (res.id) {
          this.$message.success('编辑成功')
          this.$refs.addUser.resetFields()
          this.$emit('clickId')
          this.$emit('input', false)
        } else {
          this.$message.error('错了哦，编辑失败')
        }
        console.log(res)
      } else {
        this.$emit('determine-add', this.addUser)
        this.$emit('input', false)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
